package com.hmsm.sys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hmsm.server.base.BaseService;
import com.hmsm.server.common.AllContext;
import com.hmsm.server.util.QueryResult;
import com.hmsm.sys.bean.SysMessageBean;
import com.hmsm.sys.dao.SysMessagePublishDao;
import com.hmsm.sys.po.RoleInfo;
import com.hmsm.sys.po.SysMessagePublish;
import com.hmsm.sys.po.UserInfo;

@Service
public class SysMessagePublishInfoService extends BaseService<SysMessagePublish, Integer, UserInfo, RoleInfo> {

	@Resource
	private SysMessagePublishDao sysMessagePublishDao;
	
	public SysMessagePublishDao getSysMessagePublishDao() {
		return sysMessagePublishDao;
	}
	public void setSysMessagePublishDao(SysMessagePublishDao sysMessagePublishDao) {
		this.sysMessagePublishDao = sysMessagePublishDao;
	}
	
	/**
	 * 
	 *  @Description: 查询消息列表
	 *  @MethodName :getMessageList
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-7-12 下午02:23:19 
	 *  @param sysMessagePublish
	 *  @return
	 *
	 */
	public List<SysMessagePublish> getMessageList(String title,String startTime,String endTime){
    	List<SysMessagePublish> list=this.getSysMessagePublishDao().getMessageList(title, startTime, endTime);
		List<SysMessagePublish> lists=new ArrayList<SysMessagePublish>();
		String contents="";
		for(int i=0,length=list.size();i<length;i++){
			SysMessagePublish sysMessagePublishs=list.get(i);
			contents=sysMessagePublishs.getContent();
			if(contents.length()>10){
				contents=sysMessagePublishs.getContent().substring(0,10);
				contents+=".....";
				sysMessagePublishs.setContent(contents);
			}
			lists.add(sysMessagePublishs);
		}
    	return lists;
	}
    /**
     * 添加消息
     * @author 罗超
     * @throws Exception 
     * @date 2013年6月9日 15:00:07
     */
    public SysMessagePublish addMessage(SysMessagePublish sysMessagePublish) throws Exception{
    	return this.save(sysMessagePublish);
    }
    
    /**
     * 
     *  @Description:
     *  @MethodName :getMessageList
     *  @Author     :zhangshuai
     *  @CreateDate :2013-7-22 下午01:53:52 
     *  @param index
     *  @param maxresult
     *  @param totalPage
     *  @param beginTime
     *  @param endTime
     *  @param title
     *  @param count
     *  @return
     *
     */
	public QueryResult<SysMessagePublish> getMessageList(Integer index,Integer maxresult,Integer totalPage,Date beginTime,Date endTime,String title,Integer count){
		if (index == null || maxresult == null || totalPage == null) {
			return null;
		}
		if (index<=0) {
			index = 1;
		}
		if(index>=totalPage){
			index = totalPage;
			if (index==0) {
				index=1;
			}
		}
		QueryResult<SysMessagePublish> list = new QueryResult<SysMessagePublish>();
		list.setResultlist(this.getSysMessagePublishDao().getMessageList(beginTime, endTime, title, index, maxresult));
		list.setAllPages(totalPage);
		list.setNowPageNumber(index);
		list.setNowViewRecordNumber(list.getResultlist().size());
		list.setPerPageNumber(maxresult);
		list.setTotalrecord(count);
		return list;
	}
	
	/**
	 * 
	 *  @Description:
	 *  @MethodName :getLogInfoCount
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-7-22 下午01:53:07 
	 *  @param beginTime
	 *  @param endTime
	 *  @param title
	 *  @param type
	 *  @return
	 *
	 */
	public int getMessageCount(Date beginTime,Date endTime,String title){
		return this.getSysMessagePublishDao().getMessageCount(beginTime, endTime, title);
	}
	
	@Override
	public boolean delete(SysMessagePublish entityVo) throws Exception {
		SysMessagePublish entityPo = getById(entityVo.getPkId());
		if (entityPo==null){
			throw new Exception("要删除的系统公告不存在或者已经删除！");
		}
		entityPo.setState(0);
		update(entityPo);
		return super.delete(entityPo);
	}
	
	public SysMessagePublish queryMessageTOP() {
		return getSysMessagePublishDao().querryMessageTOP();
	}
	
	public List<SysMessageBean> getScheduleMessageList(UserInfo currentUser) {
		List<SysMessageBean> scheduleMessageList = new ArrayList<SysMessageBean>();
		
		for(Entry<String, IScheduleMessage> entry:AllContext.getAppContext().getBeansOfType(IScheduleMessage.class).entrySet()){
			IScheduleMessage server = entry.getValue();
			List<SysMessageBean> list = server.getScheduleMessage(currentUser);
			if (list!=null){
				for(SysMessageBean sysMessageBean: list){
					scheduleMessageList.add(sysMessageBean);
				}
			}
		}
		
		return scheduleMessageList;
	}
	
}
